package cn.ym.org.controller;

import cn.ym.basic.exception.BusinessException;
import cn.ym.basic.utils.AjaxResult;
import cn.ym.basic.utils.ExcelUtils;
import cn.ym.basic.utils.PageList;
import cn.ym.org.domain.Shop;
import cn.ym.org.domain.ShopAuditLog;
import cn.ym.org.dto.ShopDto;
import cn.ym.org.query.ShopQuery;
import cn.ym.org.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 接口类：店铺
 */
@RestController
@RequestMapping("/shop")
@Api(value = "店铺接口" ,description = "处理店铺的所有请求")
public class ShopController {
    @Autowired
    private ShopService shopService;
    /**
     * 接口：添加或修改店铺信息
     */
    @ApiOperation(value = "添加或修改店铺信息")
    @PutMapping
    public AjaxResult save(@RequestBody  Shop shop){

            if(shop.getId()==null){
                shopService.add(shop);
            }else {
                shopService.update(shop);
            }
            return new AjaxResult();


    }

    /**
     *接口：删除单个店铺信息
     */
    @ApiOperation(value = "删除单个店铺信息")
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){

            shopService.delete(id);
            return new AjaxResult();

    }

    /**
     * 接口：查询所有店铺信息
     */
    @ApiOperation(value = "查询所有店铺信息")
    @GetMapping
    public List<Shop> findAll(){
        return shopService.findAll();
    }

    /**
     * 接口：查询单个店铺信息
     */
    @ApiOperation(value = "查询单个店铺信息")
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id") Long id){
        return shopService.findOne(id);
    }


    /**
     * 接口：批量删除店铺信息
     */
    @ApiOperation(value = "批量删除店铺信息")
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[]  ids){

            shopService.patchDel(ids);
            return new AjaxResult();

    }

    /**
     * 接口：分页查询 + 高级查询
     */
    @ApiOperation(value = "分页查询或高级查询")
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery){
        return shopService.queryPage(shopQuery);
   }



    /**
     * 接口：店铺入住
     */
    @ApiOperation(value = "店铺入住")
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){

        shopService.settlement(shop);
        return new AjaxResult();
    }


    /**
     * 接口：店铺入住-----人工审核通过
     */
    @ApiOperation(value = "审核通过")
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog auditLog) throws Exception {

        shopService.pass(auditLog);
        return new AjaxResult();
    }
    /**
     * 接口：审核成功---激活邮件
     */
    @ApiOperation(value = "审核成功")
    @GetMapping("/active/{id}")
    public String active(@PathVariable Long id){

        shopService.active(id);
        return "亲：激活成功,请点击直接去"+"<a href='http://localhost:8081/#/login'>登录</a>";
    }


    /**
     * 接口：店铺入住-----人工审核驳回
     */
    @ApiOperation(value = "驳回")
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog auditLog) throws Exception {

        shopService.reject(auditLog);
        return new AjaxResult();
    }

    /**
     * 接口：导出Excel
     */
    @ApiOperation(value = "导出Excel")
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        //查询出所有的店铺信息
        List<Shop> shops = shopService.findAll();

        ExcelUtils.exportExcel(shops,"店铺数据","店铺信息",Shop.class,"后台数据.xlsx",response);
    }

    /**
     * 接口：导入Excel
     */
    @ApiOperation(value = "导入Excel")
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Shop> shops = ExcelUtils.importExcel(file, 1, 1, Shop.class);
        for (Shop shop : shops) {
            shopService.add(shop);
        } //保存到数据库中[自己实现，提示：动态sql 批量插入]
    }


    /**
     * 接口：图形报表
     */
    @ApiOperation(value = "图形报表")
    @GetMapping("/echarts")
    public List<ShopDto> echarts(){
        return  shopService.echarts();
    }

}
